package ar.edu.untref.robotica.simulador;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.io.File;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import ar.edu.untref.robotica.log.Log;

@SuppressWarnings("serial")
public class Graficador extends JComponent implements ActionListener,
		MouseListener {

	private Escenario escenario;
	private Animador animador;
	private JFrame ventana;
	private Graphics2D graficador;
	private Set<Linea> recorrido;
	// una celda es...pixeles (del graficador, no de la imagen)
	private double relacionPixelCelda = 1;
	// UI
	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JPanel panelControlador;
	private JPanel panelGraficador;
	private JLabel datoSensorF;
	private JLabel datoSensorD;
	private JLabel datoSensorI;
	private JLabel datoSensorT;
	private JLabel datoAngulo;
	private JLabel datoVelocidadIzquierda;
	private JLabel datoVelocidadDerecha;
	private JLabel lblLog;
	private JProgressBar barraRD;
	private JProgressBar barraRI;
	private JCheckBox checkLog;
	private JTextField textMapa;
	private JButton buttonMapa;
	private JTextField textLog;
	private JLabel lblTrace;
	private JCheckBox checkTrace;
	private JComboBox<String> comboLog;
	private JLabel lblChoque;
	private JRadioButton radioSimulacion;
	private JRadioButton radioAnimacion;
	private JButton buttonNext;
	private JButton buttonPrev;
	private JButton buttonStop;
	private JButton buttonPause;
	private JButton buttonPlay;
	private JPanel panelAnimacion;
	private JLabel labelLog;

	// Imagen
	private int ancho;
	private int alto;
	private double zoomStep = 0.1;

	// Flags
	private boolean buttonMapaPressed;
	private boolean cambioLog;

	public Graficador(Escenario escenario) {

		this.escenario = escenario;
		this.animador = new Animador(escenario, this);
		this.recorrido = new HashSet<Linea>();
		configurarVentana();
		this.requestFocus();
	}

	public void configurarVentana() {

		this.ancho = this.escenario.getImagen().getWidth(null);
		this.alto = this.escenario.getImagen().getHeight(null);
		this.ventana = new JFrame();
		this.ventana.setTitle("Simulador");
		this.ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.ventana.getContentPane().add(this);
		setupUI();
		setPreferredSize(new Dimension(this.ancho, this.alto));
		this.ventana.pack();
		this.ventana.setVisible(true);
		populateComboLog();
		this.panelAnimacion.setVisible(false);
	}

	private void setupUI() {

		this.addMouseListener(this);

		this.contentPane = new JPanel();
		this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.contentPane.setLayout(new BorderLayout(0, 0));
		this.ventana.setContentPane(this.contentPane);

		this.panelGraficador = new JPanel();
		contentPane.add(panelGraficador, BorderLayout.CENTER);
		GridBagLayout gbl_panelGraficador = new GridBagLayout();
		gbl_panelGraficador.columnWidths = new int[] { 0 };
		gbl_panelGraficador.rowHeights = new int[] { 0, 0 };
		gbl_panelGraficador.columnWeights = new double[] { 1.0 };
		gbl_panelGraficador.rowWeights = new double[] { 1.0 };
		panelGraficador.setLayout(gbl_panelGraficador);

		this.panelControlador = new JPanel();
		this.panelControlador.setBorder(new TitledBorder(new LineBorder(
				new Color(128, 128, 128), 2, true), "Datos Robot",
				TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0,
						128)));
		this.contentPane.add(this.panelControlador, BorderLayout.EAST);
		GridBagLayout gbl_panelControlador = new GridBagLayout();
		gbl_panelControlador.columnWidths = new int[] { 0, 118, 0 };
		gbl_panelControlador.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0 };
		gbl_panelControlador.columnWeights = new double[] { 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_panelControlador.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE };
		this.panelControlador.setLayout(gbl_panelControlador);

		this.scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		this.panelGraficador.add(this.scrollPane, gbc_scrollPane);
		this.scrollPane.setViewportView(this);

		lblChoque = new JLabel("CHOQUE");
		lblChoque.setForeground(Color.RED);
		lblChoque.setFont(new Font("Arial", Font.BOLD, 20));

		JPanel panelChoque = new JPanel();
		GridBagConstraints gbc_panelChoque = new GridBagConstraints();
		gbc_panelChoque.anchor = GridBagConstraints.WEST;
		gbc_panelChoque.gridx = 0;
		gbc_panelChoque.gridy = 1;
		panelGraficador.add(panelChoque, gbc_panelChoque);
		panelChoque.add(lblChoque);

		panelAnimacion = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 1;
		panelGraficador.add(panelAnimacion, gbc_panel);
		GridBagLayout gbl_panelAnimacion = new GridBagLayout();
		gbl_panelAnimacion.columnWidths = new int[] { 0 };
		gbl_panelAnimacion.rowHeights = new int[] { 0 };
		gbl_panelAnimacion.columnWeights = new double[] { Double.MIN_VALUE };
		gbl_panelAnimacion.rowWeights = new double[] { Double.MIN_VALUE };
		panelAnimacion.setLayout(gbl_panelAnimacion);

		buttonPlay = new JButton();
		buttonPlay.setIcon(new ImageIcon("imageIcon/play.png"));
		buttonPlay.setFocusable(false);
		buttonPlay.setBackground(Color.BLACK);
		buttonPlay.setActionCommand("PLAY");
		buttonPlay.addActionListener(this);
		GridBagConstraints gbc_buttonPlayContainer = new GridBagConstraints();
		gbc_buttonPlayContainer.anchor = GridBagConstraints.EAST;
		gbc_buttonPlayContainer.insets = new Insets(5, 0, 0, 5);
		gbc_buttonPlayContainer.gridx = 3;
		gbc_buttonPlayContainer.gridy = 1;
		panelAnimacion.add(buttonPlay, gbc_buttonPlayContainer);

		buttonPause = new JButton();
		buttonPause.setIcon(new ImageIcon("imageIcon/pause.png"));
		buttonPause.setFocusable(false);
		buttonPause.setBackground(Color.BLACK);
		buttonPause.setActionCommand("PAUSE");
		buttonPause.addActionListener(this);
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(5, 0, 0, 5);
		gbc_btnNewButton.gridx = 4;
		gbc_btnNewButton.gridy = 1;
		panelAnimacion.add(buttonPause, gbc_btnNewButton);

		buttonStop = new JButton();
		buttonStop.setIcon(new ImageIcon("imageIcon/stop.png"));
		buttonStop.setFocusable(false);
		buttonStop.setBackground(Color.BLACK);
		buttonStop.setActionCommand("STOP");
		buttonStop.addActionListener(this);
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.insets = new Insets(5, 0, 0, 5);
		gbc_button.gridx = 5;
		gbc_button.gridy = 1;
		panelAnimacion.add(buttonStop, gbc_button);

		buttonPrev = new JButton();
		buttonPrev.setIcon(new ImageIcon("imageIcon/prev.png"));
		buttonPrev.setFocusable(false);
		buttonPrev.setBackground(Color.BLACK);
		buttonPrev.setActionCommand("PREV");
		buttonPrev.addActionListener(this);
		GridBagConstraints gbc_buttonPrevContainer = new GridBagConstraints();
		gbc_buttonPrevContainer.insets = new Insets(5, 0, 0, 5);
		gbc_buttonPrevContainer.gridx = 6;
		gbc_buttonPrevContainer.gridy = 1;
		panelAnimacion.add(buttonPrev, gbc_buttonPrevContainer);

		buttonNext = new JButton();
		buttonNext.setIcon(new ImageIcon("imageIcon/next.png"));
		buttonNext.setFocusable(false);
		buttonNext.setBackground(Color.BLACK);
		buttonNext.setActionCommand("NEXT");
		buttonNext.addActionListener(this);
		GridBagConstraints gbc_buttonNextContainer = new GridBagConstraints();
		gbc_buttonNextContainer.insets = new Insets(5, 0, 0, 5);
		gbc_buttonNextContainer.gridx = 7;
		gbc_buttonNextContainer.gridy = 1;
		panelAnimacion.add(buttonNext, gbc_buttonNextContainer);

		JPanel panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.insets = new Insets(0, 0, 5, 5);
		gbc_panel_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel_3.gridx = 0;
		gbc_panel_3.gridy = 0;
		this.panelControlador.add(panel_3, gbc_panel_3);

		JLabel lblRuedaDerecha = new JLabel("Rueda Derecha");
		lblRuedaDerecha.setForeground(new Color(0, 0, 255));
		lblRuedaDerecha.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblRuedaDerecha = new GridBagConstraints();
		gbc_lblRuedaDerecha.anchor = GridBagConstraints.WEST;
		gbc_lblRuedaDerecha.fill = GridBagConstraints.VERTICAL;
		gbc_lblRuedaDerecha.insets = new Insets(0, 0, 5, 5);
		gbc_lblRuedaDerecha.gridx = 0;
		gbc_lblRuedaDerecha.gridy = 1;
		this.panelControlador.add(lblRuedaDerecha, gbc_lblRuedaDerecha);

		this.barraRD = new JProgressBar();
		barraRD.setMinimum(-50);
		barraRD.setMaximum(50);

		GridBagConstraints gbc_barraRD = new GridBagConstraints();
		gbc_barraRD.fill = GridBagConstraints.BOTH;
		gbc_barraRD.insets = new Insets(0, 0, 5, 5);
		gbc_barraRD.gridx = 0;
		gbc_barraRD.gridy = 2;
		this.panelControlador.add(this.barraRD, gbc_barraRD);

		datoVelocidadDerecha = new JLabel();
		datoVelocidadDerecha.setForeground(Color.BLACK);
		datoVelocidadDerecha.setFont(new Font("Arial", Font.BOLD | Font.ITALIC,
				14));

		GridBagConstraints gbc_lblIzq = new GridBagConstraints();
		gbc_lblIzq.insets = new Insets(0, 0, 5, 0);
		gbc_lblIzq.gridx = 1;
		gbc_lblIzq.gridy = 2;
		this.panelControlador.add(datoVelocidadDerecha, gbc_lblIzq);

		JLabel lblRuedaIzquierda = new JLabel("Rueda Izquierda");
		lblRuedaIzquierda.setForeground(new Color(0, 0, 255));
		lblRuedaIzquierda
				.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblRuedaIzquierda = new GridBagConstraints();
		gbc_lblRuedaIzquierda.fill = GridBagConstraints.VERTICAL;
		gbc_lblRuedaIzquierda.anchor = GridBagConstraints.WEST;
		gbc_lblRuedaIzquierda.insets = new Insets(0, 0, 5, 5);
		gbc_lblRuedaIzquierda.gridx = 0;
		gbc_lblRuedaIzquierda.gridy = 3;
		this.panelControlador.add(lblRuedaIzquierda, gbc_lblRuedaIzquierda);

		this.barraRI = new JProgressBar();
		barraRI.setMinimum(-50);
		barraRI.setMaximum(50);
		GridBagConstraints gbc_barraRI = new GridBagConstraints();
		gbc_barraRI.fill = GridBagConstraints.BOTH;
		gbc_barraRI.insets = new Insets(0, 0, 0, 5);
		gbc_barraRI.gridx = 0;
		gbc_barraRI.gridy = 4;
		this.panelControlador.add(this.barraRI, gbc_barraRI);

		datoVelocidadIzquierda = new JLabel();
		datoVelocidadIzquierda.setForeground(Color.BLACK);
		datoVelocidadIzquierda.setFont(new Font("Arial", Font.BOLD
				| Font.ITALIC, 14));

		GridBagConstraints gbc_lblDer = new GridBagConstraints();
		gbc_lblDer.gridx = 1;
		gbc_lblDer.gridy = 4;
		this.panelControlador.add(datoVelocidadIzquierda, gbc_lblDer);

		JLabel lblSensorF = new JLabel("Sensor Frontal");
		lblSensorF.setForeground(new Color(0, 0, 255));
		lblSensorF.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblSensorF = new GridBagConstraints();
		gbc_lblSensorF.anchor = GridBagConstraints.WEST;
		gbc_lblSensorF.fill = GridBagConstraints.VERTICAL;
		gbc_lblSensorF.insets = new Insets(0, 0, 5, 5);
		gbc_lblSensorF.gridx = 0;
		gbc_lblSensorF.gridy = 5;
		this.panelControlador.add(lblSensorF, gbc_lblSensorF);
		this.datoSensorF = new JLabel("");
		this.datoSensorF
				.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_datoSensorF = new GridBagConstraints();
		gbc_datoSensorF.insets = new Insets(0, 0, 5, 0);
		gbc_datoSensorF.gridx = 1;
		gbc_datoSensorF.gridy = 5;
		this.panelControlador.add(this.datoSensorF, gbc_datoSensorF);

		JLabel lblSensorI = new JLabel("Sensor Izq");
		lblSensorI.setForeground(new Color(0, 0, 255));
		lblSensorI.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblSensorI = new GridBagConstraints();
		gbc_lblSensorI.anchor = GridBagConstraints.WEST;
		gbc_lblSensorI.fill = GridBagConstraints.VERTICAL;
		gbc_lblSensorI.insets = new Insets(0, 0, 5, 5);
		gbc_lblSensorI.gridx = 0;
		gbc_lblSensorI.gridy = 6;
		this.panelControlador.add(lblSensorI, gbc_lblSensorI);

		this.datoSensorI = new JLabel("");
		this.datoSensorI
				.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_datoSensorI = new GridBagConstraints();
		gbc_datoSensorI.insets = new Insets(0, 0, 5, 0);
		gbc_datoSensorI.gridx = 1;
		gbc_datoSensorI.gridy = 6;
		this.panelControlador.add(this.datoSensorI, gbc_datoSensorI);

		JLabel lblSensorD = new JLabel("Sensor Der");
		lblSensorD.setForeground(new Color(0, 0, 255));
		lblSensorD.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblSensorD = new GridBagConstraints();
		gbc_lblSensorD.anchor = GridBagConstraints.WEST;
		gbc_lblSensorD.fill = GridBagConstraints.VERTICAL;
		gbc_lblSensorD.insets = new Insets(0, 0, 5, 5);
		gbc_lblSensorD.gridx = 0;
		gbc_lblSensorD.gridy = 7;
		this.panelControlador.add(lblSensorD, gbc_lblSensorD);

		this.datoSensorD = new JLabel("");
		this.datoSensorD
				.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_datoSensorD = new GridBagConstraints();
		gbc_datoSensorD.insets = new Insets(0, 0, 5, 0);
		gbc_datoSensorD.gridx = 1;
		gbc_datoSensorD.gridy = 7;
		this.panelControlador.add(this.datoSensorD, gbc_datoSensorD);

		JLabel lblSensorT = new JLabel("Sensor Trasero");
		lblSensorT.setForeground(new Color(0, 0, 205));
		lblSensorT.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblSensorT = new GridBagConstraints();
		gbc_lblSensorT.anchor = GridBagConstraints.WEST;
		gbc_lblSensorT.fill = GridBagConstraints.VERTICAL;
		gbc_lblSensorT.insets = new Insets(0, 0, 5, 5);
		gbc_lblSensorT.gridx = 0;
		gbc_lblSensorT.gridy = 8;
		this.panelControlador.add(lblSensorT, gbc_lblSensorT);

		this.datoSensorT = new JLabel("");
		this.datoSensorT
				.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_datoSensorT = new GridBagConstraints();
		gbc_datoSensorT.insets = new Insets(0, 0, 5, 0);
		gbc_datoSensorT.gridx = 1;
		gbc_datoSensorT.gridy = 8;
		this.panelControlador.add(this.datoSensorT, gbc_datoSensorT);

		JLabel lblAngulo = new JLabel("Angulo");
		lblAngulo.setForeground(new Color(0, 0, 255));
		lblAngulo.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_lblAngulo = new GridBagConstraints();
		gbc_lblAngulo.anchor = GridBagConstraints.WEST;
		gbc_lblAngulo.fill = GridBagConstraints.VERTICAL;
		gbc_lblAngulo.insets = new Insets(0, 0, 5, 5);
		gbc_lblAngulo.gridx = 0;
		gbc_lblAngulo.gridy = 9;
		this.panelControlador.add(lblAngulo, gbc_lblAngulo);

		this.datoAngulo = new JLabel("");
		this.datoAngulo.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_datoAngulo = new GridBagConstraints();
		gbc_datoAngulo.insets = new Insets(0, 0, 5, 0);
		gbc_datoAngulo.gridx = 1;
		gbc_datoAngulo.gridy = 9;
		this.panelControlador.add(this.datoAngulo, gbc_datoAngulo);

		lblTrace = new JLabel("Trace");
		lblTrace.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		lblTrace.setForeground(Color.BLUE);
		GridBagConstraints gbc_traceContainer = new GridBagConstraints();
		gbc_traceContainer.anchor = GridBagConstraints.WEST;
		gbc_traceContainer.fill = GridBagConstraints.VERTICAL;
		gbc_traceContainer.insets = new Insets(0, 0, 5, 5);
		gbc_traceContainer.gridx = 0;
		gbc_traceContainer.gridy = 10;
		this.panelControlador.add(lblTrace, gbc_traceContainer);

		checkTrace = new JCheckBox();
		checkTrace.setSelected(true);
		GridBagConstraints gbc_checkTraceContainer = new GridBagConstraints();
		gbc_checkTraceContainer.insets = new Insets(0, 0, 5, 0);
		gbc_checkTraceContainer.gridx = 0;
		gbc_checkTraceContainer.gridy = 10;
		this.panelControlador.add(checkTrace, gbc_checkTraceContainer);

		lblLog = new JLabel("Activar Log");
		lblLog.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		lblLog.setForeground(Color.BLUE);

		GridBagConstraints gbc_logContainer = new GridBagConstraints();
		gbc_logContainer.anchor = GridBagConstraints.WEST;
		gbc_logContainer.fill = GridBagConstraints.HORIZONTAL;
		gbc_logContainer.insets = new Insets(0, 0, 5, 5);
		gbc_logContainer.gridx = 0;
		gbc_logContainer.gridy = 11;
		this.panelControlador.add(lblLog, gbc_logContainer);

		checkLog = new JCheckBox();
		checkLog.setSelected(false);
		checkLog.setActionCommand("CHECK LOG");
		checkLog.addActionListener(this);
		GridBagConstraints gbc_checkContainer = new GridBagConstraints();
		gbc_checkContainer.insets = new Insets(0, 0, 5, -60);
		gbc_checkContainer.gridx = 0;
		gbc_checkContainer.gridy = 11;
		this.panelControlador.add(checkLog, gbc_checkContainer);

		textLog = new JTextField();
		textLog.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		textLog.setText(Configuracion.instancia().getRutaDelLog());
		GridBagConstraints gbc_textLogContainer = new GridBagConstraints();
		gbc_textLogContainer.anchor = GridBagConstraints.WEST;
		gbc_textLogContainer.fill = GridBagConstraints.BOTH;
		gbc_textLogContainer.insets = new Insets(0, 0, 5, 5);
		gbc_textLogContainer.gridx = 1;
		gbc_textLogContainer.gridy = 11;
		this.panelControlador.add(textLog, gbc_textLogContainer);

		labelLog = new JLabel("Logs");
		labelLog.setForeground(Color.BLUE);
		labelLog.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		GridBagConstraints gbc_labelLog = new GridBagConstraints();
		gbc_labelLog.anchor = GridBagConstraints.WEST;
		gbc_labelLog.fill = GridBagConstraints.VERTICAL;
		gbc_labelLog.insets = new Insets(0, 0, 5, 5);
		gbc_labelLog.gridx = 0;
		gbc_labelLog.gridy = 12;
		panelControlador.add(labelLog, gbc_labelLog);

		comboLog = new JComboBox<String>();
		comboLog.addActionListener(this);
		comboLog.setActionCommand("COMBO");
		GridBagConstraints gbc_comboLogContainer = new GridBagConstraints();
		gbc_comboLogContainer.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboLogContainer.insets = new Insets(0, 0, 5, 5);
		gbc_comboLogContainer.gridx = 1;
		gbc_comboLogContainer.gridy = 12;
		panelControlador.add(comboLog, gbc_comboLogContainer);

		textMapa = new JTextField();
		textMapa.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		textMapa.setText(Configuracion.instancia().getRutaDelMapa());
		GridBagConstraints gbc_textMapaContainer = new GridBagConstraints();
		gbc_textMapaContainer.fill = GridBagConstraints.HORIZONTAL;
		gbc_textMapaContainer.insets = new Insets(0, 0, 5, 5);
		gbc_textMapaContainer.gridx = 1;
		gbc_textMapaContainer.gridy = 14;
		this.panelControlador.add(textMapa, gbc_textMapaContainer);

		buttonMapa = new JButton("Cargar mapa");
		buttonMapa.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		buttonMapa.setBackground(Color.BLACK);
		buttonMapa.setForeground(Color.BLUE);
		buttonMapa.setFocusable(false);
		buttonMapa.setActionCommand("RELOAD MAP");
		buttonMapa.addActionListener(this);
		GridBagConstraints gbc_buttonMapaContainer = new GridBagConstraints();
		gbc_buttonMapaContainer.anchor = GridBagConstraints.WEST;
		gbc_buttonMapaContainer.fill = GridBagConstraints.VERTICAL;
		gbc_buttonMapaContainer.insets = new Insets(0, 0, 5, 5);
		gbc_buttonMapaContainer.gridx = 0;
		gbc_buttonMapaContainer.gridy = 14;
		this.panelControlador.add(buttonMapa, gbc_buttonMapaContainer);

		radioSimulacion = new JRadioButton("Modo simulación");
		radioSimulacion.addActionListener(this);
		radioSimulacion.setActionCommand("SIMULACION");
		radioSimulacion.setFocusable(false);
		GridBagConstraints gbc_radioSimulacionContainer = new GridBagConstraints();
		gbc_radioSimulacionContainer.anchor = GridBagConstraints.WEST;
		gbc_radioSimulacionContainer.fill = GridBagConstraints.VERTICAL;
		gbc_radioSimulacionContainer.insets = new Insets(0, 0, 5, 5);
		gbc_radioSimulacionContainer.gridx = 0;
		gbc_radioSimulacionContainer.gridy = 15;
		this.panelControlador
				.add(radioSimulacion, gbc_radioSimulacionContainer);

		radioAnimacion = new JRadioButton("Modo animación");
		radioAnimacion.addActionListener(this);
		radioAnimacion.setActionCommand("ANIMACION");
		radioAnimacion.setFocusable(false);
		GridBagConstraints gbc_radioAnimacionContainer = new GridBagConstraints();
		gbc_radioAnimacionContainer.anchor = GridBagConstraints.WEST;
		gbc_radioAnimacionContainer.fill = GridBagConstraints.VERTICAL;
		gbc_radioAnimacionContainer.insets = new Insets(0, 0, 5, 5);
		gbc_radioAnimacionContainer.gridx = 1;
		gbc_radioAnimacionContainer.gridy = 15;
		this.panelControlador.add(radioAnimacion, gbc_radioAnimacionContainer);

		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(radioSimulacion);
		buttonGroup.add(radioAnimacion);
		radioSimulacion.setSelected(true);
	}

	public boolean modoSimulacion() {

		return this.radioSimulacion.isSelected();
	}

	@Override
	public void paint(Graphics graficador) {

		this.graficador = (Graphics2D) graficador;
		AffineTransform escala = new AffineTransform();
		escala.scale(this.relacionPixelCelda, this.relacionPixelCelda);
		this.graficador.transform(escala);
		dibujarImagen();
		dibujarRobot();
		djbujarRecorrido();
	}

	private void djbujarRecorrido() {
		if (isTraceEnabled()) {
			Point posicion = new Point((int) this.escenario.getPosicionRobot()
					.getPosicionX(), (int) this.escenario.getPosicionRobot()
					.getPosicionY());
			this.recorrido.add(new Linea(posicion, posicion));
			this.graficador.setPaint(Color.red);
			for (Line2D.Double cadaLinea : this.recorrido) {
				this.graficador.draw(cadaLinea);
			}
		}
	}

	private void dibujarImagen() {

		Image imagen = this.escenario.getImagen();
		this.graficador.drawImage(imagen, 0, 0, null);
	}

	public void dibujarRobot() {

		this.graficador.setPaint(Color.blue);
		this.graficador.fill(this.escenario.getShapeRobot());
		this.graficador.setPaint(Color.cyan);
		this.graficador.fill(this.escenario.getIndicacionRobot());
	}

	public void aumentarZoom() {

		this.relacionPixelCelda = this.relacionPixelCelda + this.zoomStep;
		setPreferredSize(new Dimension(
				(int) (this.ancho * this.relacionPixelCelda),
				(int) (this.alto * this.relacionPixelCelda)));
		this.scrollPane.getViewport().revalidate();
	}

	public void disminuirZoom() {

		this.relacionPixelCelda = this.relacionPixelCelda - this.zoomStep;
		setPreferredSize(new Dimension(
				(int) (this.ancho * this.relacionPixelCelda),
				(int) (this.alto * this.relacionPixelCelda)));
		this.scrollPane.getViewport().revalidate();
	}

	public void sensar(Robot robot) {

		Double valorFrontal = robot
				.getDistanciaObstaculoAdelante(this.escenario);
		Double valorIzq = robot
				.getDistanciaObstaculo45Izquierda(this.escenario);
		Double valorDer = robot.getDistanciaObstaculo45Derecha(this.escenario);
		Double valorTrasero = robot.getDistanciaObstaculoAtras(this.escenario);
		Double valorAngulo = this.escenario.getPosicionRobot().getAngulo();
		Float vF = valorFrontal.floatValue();
		Float vI = valorIzq.floatValue();
		Float vD = valorDer.floatValue();
		Float vT = valorTrasero.floatValue();
		Integer vA = valorAngulo.intValue();
		BigDecimal vRI = robot.getRuedaI().getVelocidad();
		BigDecimal vRD = robot.getRuedaD().getVelocidad();
		this.datoSensorF.setText(vF.toString());
		this.datoSensorI.setText(vI.toString());
		this.datoSensorD.setText(vD.toString());
		this.datoSensorT.setText(vT.toString());
		this.datoAngulo.setText(vA.toString());
		this.datoVelocidadIzquierda.setText(Float.toString(vRI.floatValue()));
		this.datoVelocidadDerecha.setText(Float.toString(vRD.floatValue()));
		this.barraRI.setValue(vRI.intValue());
		this.barraRD.setValue(vRD.intValue());

	}

	public String[] datosRelevantesAGuardar() {

		String[] datos = new String[9];
		datos[0] = this.datoAngulo.getText();
		datos[1] = Double.toString(this.escenario.getPosicionRobot()
				.getPosicionX());
		datos[2] = Double.toString(this.escenario.getPosicionRobot()
				.getPosicionY());
		datos[3] = this.datoVelocidadDerecha.getText();
		datos[4] = this.datoVelocidadIzquierda.getText();
		datos[5] = this.datoSensorF.getText();
		datos[6] = this.datoSensorI.getText();
		datos[7] = this.datoSensorD.getText();
		datos[8] = this.datoSensorT.getText();

		return datos;
	}

	public JProgressBar getBarraDer() {

		return this.barraRD;
	}

	public JProgressBar getBarraIzq() {

		return this.barraRI;
	}

	public boolean isLogEnabled() {
		return this.checkLog.isSelected();
	}

	public boolean isTraceEnabled() {
		return this.checkTrace.isSelected();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String name = e.getActionCommand();
		switch (name) {
		case "RELOAD MAP":
			this.buttonMapaPressed = true;
			break;
		case "CHECK LOG":
			textLog.setEnabled(!checkLog.isSelected());
			if (checkLog.isSelected()) {
				this.cambioLog = true;
				if (!textLog.getText().equals(
						Configuracion.instancia().getRutaDelLog())) {
					Configuracion.instancia().setRutaDelLog(textLog.getText());
				}
			} else {
				this.populateComboLog();
			}
			break;
		case "PLAY":
			this.animador.play();
			break;
		case "PAUSE":
			this.animador.pause();
			break;
		case "PREV":
			this.animador.prev();
			break;
		case "NEXT":
			this.animador.next();
			break;
		case "STOP":
			this.animador.stop();
			this.recorrido.clear();
			break;
		case "COMBO":
			if (this.comboLog.getSelectedItem() != null) {
				this.animador.setAnimacion(Log.parsearFile(this.textLog
						.getText()
						+ "/"
						+ (String) this.comboLog.getSelectedItem()));
				if (!modoSimulacion()) {
					this.animador.stop();
					this.recorrido.clear();
				}
			}
			break;
		case "ANIMACION":
			if (this.checkLog.isSelected()) {
				this.populateComboLog();
			}
			this.checkLog.setEnabled(false);
			this.checkLog.setSelected(false);
			this.textLog.setEnabled(false);
			if (this.comboLog.getSelectedItem() != null) {
				this.panelAnimacion.setVisible(true);
			}
			break;
		case "SIMULACION":
			this.checkLog.setEnabled(true);
			this.textLog.setEnabled(true);
			this.panelAnimacion.setVisible(false);
			break;
		}
	}

	public boolean cambioMapa() {

		boolean cambioMapa = this.buttonMapaPressed;
		if (cambioMapa) {
			this.buttonMapaPressed = false;
		}
		return cambioMapa;
	}

	public boolean cambioLog() {

		boolean cambioLog = this.cambioLog;
		if (cambioLog) {
			this.cambioLog = false;
		}
		return cambioLog;
	}

	public String getNuevoMapa() {

		return textMapa.getText();
	}

	public void setEscenario(Escenario escenario) {

		this.escenario = escenario;
	}

	@Override
	public void mouseClicked(MouseEvent e) {

		this.requestFocus();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	private void populateComboLog() {

		comboLog.removeAllItems();
		File[] files = new File(textLog.getText()).listFiles();
		for (File file : files) {
			if (file.getName().endsWith(".txt")) {
				this.comboLog.addItem(file.getName());
			}
		}
	}

	public void choque(boolean choque) {

		this.lblChoque.setVisible(choque);
	}
}
